package week_eight;

import java.util.HashMap;
import java.util.Map;

public class IsOmorphicStrings_205 {
    public boolean isIsomorphic1(String s, String t) {
        Map<Character, Character> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char left = s.charAt(i);
            char right = t.charAt(i);
            if (map.containsKey(left)) {
                if (map.get(left) != right) return false;
            } else {
                if (map.containsValue(right)) return false;
                map.put(left, right);
            }
        }
        return true;
    }

    public boolean isIsomorphic(String s, String t) {
        Map<Character, Character> s2t = new HashMap<>();
        Map<Character, Character> t2s = new HashMap<>();

        for (int i = 0; i < s.length(); i++) {
            char left = s.charAt(i);
            char right = t.charAt(i);
            if ((s2t.containsKey(left) && s2t.get(left) != right) || (t2s.containsKey(right) && t2s.get(right) != left)) {
                return false;
            } else {
                s2t.put(left, right);
                t2s.put(right, left);
            }
        }
        return true;
    }
}
